using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ShowImage
{
    public partial class Form1 : Form
    {
        private Bitmap bitmap;
        string path = "";
        string fileDel = "";
        string[] files;

        int nSelectedFile = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnPath_Click(object sender, EventArgs e)
        {

            this.openFileDialog1.Multiselect = true;
            this.openFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF;*PNG)|*.BMP;*.JPG;*.GIF;*.PNG";

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                files = openFileDialog1.FileNames;
                this.listView1.Clear();

                //add listview
                this.listView1.Dock = System.Windows.Forms.DockStyle.Left;
                this.listView1.Location = new System.Drawing.Point(0, 0);
                this.listView1.Name = "listView1";
                this.listView1.Size = new System.Drawing.Size(96, 352);
                this.listView1.TabIndex = 4;
                this.listView1.UseCompatibleStateImageBehavior = false;
                this.listView1.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listView1_ItemSelectionChanged);
                this.listView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListView1_MouseDown);

                string a = openFileDialog1.FileName;
                path = a.Substring(0, a.LastIndexOf("\\"));
                this.btnFirst.Enabled = true;
                this.btnLast.Enabled = true;
                this.btnNext.Enabled = true;
                this.btnPrevious.Enabled = true;
                this.btnLeft.Enabled = true;
                this.btnRight.Enabled = true;
                this.button1.Enabled = true;
                this.button2.Enabled = true;
                this.btnZoomIn.Enabled = true;
                this.btnZoomOut.Enabled = true;

            }
            PopulateListView();

        }

        private void PopulateListView()
        {

            ColumnHeader header1, header2;
            header1 = new ColumnHeader();
            header2 = new ColumnHeader();

            // Set the text, alignment and width for each column header.
            header1.Text = "File name";
            header1.TextAlign = HorizontalAlignment.Left;
            header1.Width = 70;

            header2.TextAlign = HorizontalAlignment.Left;
            header2.Text = "Location";
            header2.Width = 200;


            // Add the headers to the ListView control.
            listView1.Columns.Add(header1);
            listView1.Columns.Add(header2);

            // Add each file name and full name including path
            // to the ListView.
            if (files != null)
            {
                foreach (string sfile in files)
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(sfile);
                    ListViewItem item = new ListViewItem(file.Name);
                    item.SubItems.Add(file.FullName);
                    listView1.Items.Add(item);
                    nSelectedFile = listView1.Items[0].Index;
                    
                }
            }
            
        }

        private void InitializePictureBox()
        {
            pictureBox1 = new PictureBox();

            // Set the location and size of the PictureBox control.
            //this.pictureBox1.Location = new System.Drawing.Point(70, 120);
            //this.pictureBox1.Size = new System.Drawing.Size(140, 140);
            this.pictureBox1.TabStop = false;


            // Set the SizeMode property to the StretchImage value.  This
            // will shrink or enlarge the image as needed to fit into
            // the PictureBox.
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            // Set the border style to a three-dimensional border.
            this.pictureBox1.BorderStyle = BorderStyle.Fixed3D;

            // Add the PictureBox to the form.
            this.Controls.Add(this.pictureBox1);

        }


        private void ListView1_MouseDown(object sender, MouseEventArgs e)
        {
            ListViewItem selection = listView1.GetItemAt(e.X, e.Y);
            this.pictureBox1.BackgroundImage = null;
            if (selection != null)
            {
                bitmap = (Bitmap)System.Drawing.Image.FromFile(selection.SubItems[1].Text);

                fileDel = selection.SubItems[1].Text;
                this.btnDelete.Enabled = true;
                pictureBox1.Width = bitmap.Width;
                pictureBox1.Height = bitmap.Height;

                pictureBox1.BackgroundImage = (Image)bitmap;
            }
            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            pictureBox1.BackgroundImage = null;
            nSelectedFile = e.ItemIndex;
            bitmap = (Bitmap)System.Drawing.Image.FromFile(Path.Combine(path, GetFilePath()));
            pictureBox1.BackgroundImage = bitmap;
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = null;
            if (bitmap != null)
            {
                //bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                //bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                //bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.BackgroundImage = bitmap;
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = null;
            if (bitmap != null)
            {
                //bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                pictureBox1.BackgroundImage = bitmap;
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = null;
            if (bitmap != null)
            {
                //bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY);

                //bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
                pictureBox1.BackgroundImage = bitmap;
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = null;
            if (bitmap != null)
            {
                //bitmap.RotateFlip(RotateFlipType.Rotate180FlipY);
                pictureBox1.BackgroundImage = bitmap;
                pictureBox1.BackgroundImage.RotateFlip(RotateFlipType.Rotate180FlipY);
            }
        }

        private string GetFilePath()
        {
            if (nSelectedFile > listView1.Items.Count) nSelectedFile = 0;
            return listView1.Items[nSelectedFile].Text;
        }

        private void btnLast_Click(object sender, EventArgs e)
        {

            this.btnDelete.Enabled = true;

            pictureBox1.BackgroundImage = null;

            nSelectedFile = listView1.Items.Count - 1;
            string file = GetFilePath();
            bitmap = (Bitmap)System.Drawing.Image.FromFile(Path.Combine(path, file));
            pictureBox1.Width = bitmap.Width;
            pictureBox1.Height = bitmap.Height;
            pictureBox1.BackgroundImage = bitmap;
            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
            fileDel = path + file;
        }


        private void btnNext_Click(object sender, EventArgs e)
        {

            this.btnDelete.Enabled = true;

            pictureBox1.BackgroundImage = null;

            if (nSelectedFile != -1)
            {
                nSelectedFile++;
                if (nSelectedFile >= listView1.Items.Count)
                    nSelectedFile = 0;
                string file = GetFilePath();
                bitmap = (Bitmap)System.Drawing.Image.FromFile(Path.Combine(path, file));

                pictureBox1.BackgroundImage = null;
                fileDel = path + file;
                pictureBox1.Width = bitmap.Width;
                pictureBox1.Height = bitmap.Height;
                pictureBox1.BackgroundImage = bitmap;
            }           

            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bitmap = (Bitmap)System.Drawing.Image.FromFile("TeamSoft.jpg");
            pictureBox1.Width = bitmap.Width;
            pictureBox1.Height = bitmap.Height;
            pictureBox1.BackgroundImage = bitmap;
            pictureBox1.Anchor = AnchorStyles.None;
            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
        }


        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Width < 2048)
                this.pictureBox1.Width += 50;
            if (pictureBox1.Height < 1536)
                this.pictureBox1.Height += 50;

            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
        }


        private void btnPrevious_Click(object sender, EventArgs e)
        {

            this.btnDelete.Enabled = true;

            pictureBox1.BackgroundImage = null;

            if (nSelectedFile != -1)
            {
                nSelectedFile--;
                if (nSelectedFile < 0)
                    nSelectedFile = listView1.Items.Count - 1;
                string file = GetFilePath();
                bitmap = (Bitmap)System.Drawing.Image.FromFile(Path.Combine(path, file));
                pictureBox1.Width = bitmap.Width;
                pictureBox1.Height = bitmap.Height;
                pictureBox1.BackgroundImage = bitmap;
                fileDel = path + file;
            }
            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));

        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            this.btnDelete.Enabled = true;

            pictureBox1.BackgroundImage = null;

            nSelectedFile = 0;
            string file = GetFilePath();
            bitmap = (Bitmap)System.Drawing.Image.FromFile(Path.Combine(path, file));
            pictureBox1.Width = bitmap.Width;
            pictureBox1.Height = bitmap.Height;
            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
            pictureBox1.BackgroundImage = bitmap;

            fileDel = path + file;
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {

            if (this.pictureBox1.Width > 50)
                this.pictureBox1.Width -= 50;
            if (this.pictureBox1.Height > 50)
                this.pictureBox1.Height -= 50;

            pictureBox1.Location = new System.Drawing.Point((panelImage.Width / 2) - (pictureBox1.Width / 2), (panelImage.Height / 2) - (pictureBox1.Height / 2));
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (fileDel != "")
            {
                if (MessageBox.Show("Do you want delete this file ?", "TeamSoft - QLCH", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    bitmap.Dispose();
                    this.pictureBox1.BackgroundImage = null;
                    File.Delete(fileDel);
                }
            }
        }        
      

        private void btnAbout_Click(object sender, EventArgs e)
        {
            About dlg = new About();
            dlg.ShowDialog();
        }
    }
}